home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / educate / wordy442.zip / BINGO.C < prev    next >
C/C++ Source or Header  |  1996-06-20  |  7KB  |  258 lines

  1. /**************************************************************************/
  2. /*                                  Bingo Utility                         */
  3. /*                                                                        */
  4. /*                                                                        */
  5. /*                                     M\Cooper                           */
  6. /*                                    PO Box 237                          */
  7. /*                            St. David, AZ 85630-0237                    */
  8. /*                        -------------------------------                 */
  9. /*                        Email:  thegrendel@theriver.com                 */
  10. /*                                                                        */
  11. /*                   $2.00 to register the entire WORDY package           */
  12. /*                                                                        */
  13. /**************************************************************************/
  14.  
  15.  
  16. #include <conio.h>
  17. #include "srch.h"
  18.  
  19.  
  20. #define FILE_OPENING_ERROR 3
  21. #define FILENAME_MAXLEN 8
  22. #define CR "\n"
  23. #define FILE_SUFFIX ".bgo"
  24. #define MAXLEN 30
  25. #define LINE_LEN 80
  26. #define NOARGS 1
  27. #define FILEARGS 3
  28. #define INCREMENT 1
  29. #define SPACE ' '
  30. #define XOUT '@'
  31. #define WILDCARD '?'
  32. #define FILLCHAR '_'
  33. #define BINGO 7
  34. #define BUFFERSIZE 8192
  35.  
  36. char ad[] =
  37. "BINGO utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  38.  
  39.  
  40. void getword( char *lset, char *file_name );
  41. void center( char *strng );
  42.  
  43. typedef enum { FALSE, TRUE } Boolean;
  44.  
  45. void main( int argc, char **argv )
  46. {
  47.  
  48.    char letterset[ MAXLEN ],
  49.         filename [ MAXLEN ];
  50.  
  51.      if( argc == NOARGS )
  52.         {
  53.         clrscr();
  54.         puts("Enter a LETTERSET to test ... ");
  55.         gets( letterset );
  56.         }
  57.      else
  58.         strcpy( letterset, *( argv + 1 ) );
  59.  
  60.      if( argc == FILEARGS ) 
  61.          strcpy( filename, *( argv + 2 ) );
  62.      else
  63.          strcpy( filename, "word.lst" );
  64.  
  65.      getword( letterset, filename );
  66. }
  67.  
  68.  
  69.  
  70. /**********************************WORDTEST********************************/
  71. /*       Function tests if word is constructible from Letterset           */
  72. /*                 Args in: char *letterset, char *word                   */
  73. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  74. /**************************************************************************/
  75.  
  76. Boolean wordtest( char *letterset, char *word )
  77. {
  78.     Boolean error_flag = TRUE;
  79.     static char dup_lset[ MAXLEN ];
  80.     register char *letpos;
  81.  
  82.      strcpy( dup_lset, letterset );
  83.          
  84.         while( *word )
  85.             {
  86.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  87.                 *letpos = XOUT;     //As long as letter contained...
  88.       else
  89.          if( ( letpos = strchr( dup_lset, WILDCARD ) ) != NULL ) 
  90.             *letpos = XOUT;  //Or wildcard character...
  91.  
  92.             else
  93.                 { error_flag = FALSE; break; } //test fails (not contained)
  94.             }
  95.  
  96.         return( error_flag );
  97. }
  98.  
  99. /*************************************************************/
  100. void getword( char *letter_set, char *file_name )
  101. {
  102.  
  103.     char    l_set [ MAXLEN ],
  104.         word [ MAXLEN ],
  105.         tempstr [ MAXLEN + 1 ],
  106.         targetfile [ MAXLEN ],
  107.         bar [ LINE_LEN + 1 ],
  108.         double_bar [ LINE_LEN + 1 ],
  109.    Bnum [ BINGO + 2 ],
  110.    *ppos;
  111.  
  112.     FILE *fptr,
  113.         *tfile;
  114.     int fnamelen,
  115.      i;
  116.     long wcount = 0L;
  117.  
  118.        memset( bar, '-', LINE_LEN );
  119.        *( bar + LINE_LEN ) = NULL;
  120.        memset( double_bar, '=', LINE_LEN );
  121.        *( double_bar + LINE_LEN ) = NULL;
  122.  
  123.        /*************opening credits*************/
  124.        clrscr();
  125.        printf( double_bar );
  126.        strcpy( tempstr, ad );
  127.        center ( tempstr );
  128.        printf( tempstr );
  129.     printf( CR );
  130. //  printf( " " );  //Pad out line.
  131.        printf( double_bar );
  132.        printf( CR );
  133.        /****************************************/
  134.  
  135.  
  136.        strcpy ( l_set, letter_set );
  137.        strcat ( letter_set, CR );
  138.  
  139.        /*   Create name of file to store derived words in   */
  140.        /*********************************************************/
  141.        fnamelen = strlen( l_set );
  142.        if( fnamelen  > FILENAME_MAXLEN )
  143.           fnamelen = FILENAME_MAXLEN;
  144.        strncpy( targetfile, l_set, fnamelen );
  145.        *( targetfile + fnamelen ) = NULL;
  146.        //NULL-terminate string, so strcat works, ha, ha.
  147.  
  148.       for( i = 0; i < fnamelen; i++ )
  149.          if( *( targetfile + i ) == WILDCARD )
  150.              *( targetfile + i ) = FILLCHAR;
  151.  
  152.  
  153. //    if( ( ppos = strchr( targetfile, '?' ) ) != NULL ) 
  154. //          *ppos = '0';
  155. //           strcpy( targetfile, "wildcard" );
  156.  
  157. //    ppos = targetfile;
  158. //    while( *ppos != NULL )
  159. //       if( *ppos == '?' )
  160. //          {
  161. //          *ppos = FILLCHAR;
  162. //          ppos++;
  163. //          printf( "\7\7\7\7" );  /******DEBUG******/
  164. //          }
  165.  
  166.        strcat( targetfile, FILE_SUFFIX );
  167.        /*********************************************************/
  168.  
  169.        if( !( fptr = fopen( file_name, "rt" ) ) )
  170.          {
  171.          printf( "\7\7\7Cannot open Wordfile: %s!", file_name );
  172.          exit( FILE_OPENING_ERROR );
  173.          }
  174.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  175.          exit( FILE_OPENING_ERROR );
  176.  
  177.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  178.          {
  179.          printf( "\7\7\7Cannot open file to save words in!" );
  180.          exit ( FILE_OPENING_ERROR + 2 );
  181.          }
  182.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  183.          exit( FILE_OPENING_ERROR );
  184.  
  185.        /**************'Wait' Message***************/
  186.        printf( CR CR );
  187.        printf( "WORKING...\n\n" );
  188.        printf( "This will take a few seconds...\n" );
  189.        printf( "Please be patient.\n\n" );
  190.        printf( "Now searching 100,000+ word file and writing file of valid bingos.\n\n" );
  191.        /********************************************/
  192.  
  193.  
  194.  
  195.  
  196.  
  197.        sprintf( tempstr, "Bingo(s) created from: %s\n", strupr( l_set ) );
  198.        center( tempstr );
  199.        fprintf( tfile, double_bar );
  200.       fprintf( tfile, CR );
  201.        fprintf( tfile, tempstr );
  202.        fprintf( tfile, double_bar );
  203.        fprintf( tfile, CR );
  204.  
  205.  
  206.          /*********************Main Loop*************/     
  207.           while( fgets( word, MAXLEN, fptr ) != NULL )
  208.  
  209.             if( wordtest( letter_set, word ) && strlen( word ) >= BINGO + 1 ) // >7
  210.                {
  211.                fprintf( tfile, "%s", word );
  212.                wcount++;
  213.                }
  214.           /*******************************************/
  215.  
  216.       if( wcount == INCREMENT )
  217.          strcpy( Bnum, "Bingo" );
  218.       else
  219.          strcpy( Bnum, "Bingos" );
  220.  
  221.           fprintf( tfile, bar );
  222.       fprintf( tfile, CR );
  223.           sprintf( tempstr, "%ld %s can be constructed from %s.",
  224.                  wcount, Bnum, l_set );
  225.           center( tempstr );              
  226.           fprintf( tfile, tempstr );
  227.           fprintf( tfile, "\n\n" );
  228.  
  229.           center( ad );
  230.           fprintf( tfile, ad );
  231.  
  232.           fcloseall();
  233.  
  234.           sprintf( tempstr,
  235.                  "The file %s has %ld %s derived from %s\7.",
  236.                  targetfile, wcount, Bnum, l_set );
  237.           center( tempstr );
  238.           printf( CR CR );
  239.           printf( tempstr );
  240.  
  241. }
  242.  
  243.  
  244.  
  245. void center( char *str )
  246. {
  247.    int padding;
  248.    char st [ LINE_LEN + INCREMENT ];
  249.  
  250.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  251.      memset( st, SPACE, padding );
  252.      *( st + padding ) = NULL;  //Terminate string
  253.      strcat( st, str );
  254.      strcpy( str, st );
  255.  
  256.      return;
  257. }
  258.